package com.zzl.finance.data.manamger;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzl.finance.data.constant.ApproveStatusConstant;
import com.zzl.finance.data.constant.PersonTypeEnum;
import com.zzl.finance.data.dto.EnterpriseAddDto;
import com.zzl.finance.data.entity.EnterpriseEntity;
import com.zzl.finance.data.entity.EnterprisePersonEntity;
import com.zzl.finance.data.service.EnterprisePersonService;
import com.zzl.finance.data.service.EnterpriseService;
import com.zzl.general.common.exception.SysException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：hyt
 * @description：企业通用逻辑处理
 * @date ：2021/12/7 13:24
 */
@Slf4j
@Component
public class EnterpriseManager {

    @Autowired
    private EnterprisePersonService enterprisePersonService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 企业id获取法人信息
     *
     * @param enterpriseIds
     */
    public List<EnterprisePersonEntity> getLegalByEnterpriseId(List<String> enterpriseIds) {
        QueryWrapper<EnterprisePersonEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(EnterprisePersonEntity::getEnterpriseId, enterpriseIds)
                .eq(EnterprisePersonEntity::getPersonnelType, PersonTypeEnum.PERSON_TYPE_LEGAL.getCode());
        return enterprisePersonService.list(queryWrapper);
    }

    /**
     * 企业名称模糊搜索企业
     */
    public List<EnterpriseEntity> listByEnterpriseName(String enterpriseName) {
        QueryWrapper<EnterpriseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(EnterpriseEntity::getEnterpriseName, enterpriseName)
                .eq(EnterpriseEntity::getApproveStatus, ApproveStatusConstant.APPROVE_STATUS_AGREE.getKey());
        return enterpriseService.list(queryWrapper);
    }

    /**
     * 企业名称模糊搜索企业ids
     *
     * @return
     */
    public List<String> listIdsByEnterpriseName(String enterpriseName) {
        List<EnterpriseEntity> enterprises = listByEnterpriseName(enterpriseName);
        if (CollectionUtils.isEmpty(enterprises)) {
            return new ArrayList<>();
        } else {
            return enterprises.stream().map(EnterpriseEntity::getId).collect(Collectors.toList());
        }
    }

    /**
     * 企业ids获取企业信息s
     *
     * @param enterpriseIds 企业ids
     */
    public List<EnterpriseEntity> listByIds(List<String> enterpriseIds) {
        QueryWrapper<EnterpriseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(EnterpriseEntity::getId, enterpriseIds)
                .eq(EnterpriseEntity::getApproveStatus, ApproveStatusConstant.APPROVE_STATUS_AGREE.getKey());
        return enterpriseService.list(queryWrapper);
    }

    /**
     * 新增企业dto
     *
     * @param enterpriseAddDto 新增企业dto
     * @return
     */
    public String saveEnterpriseReturnId(EnterpriseAddDto enterpriseAddDto) throws SysException {
        EnterpriseEntity enterprise = new EnterpriseEntity();
        BeanUtils.copyProperties(enterpriseAddDto, enterprise);
        enterprise.setApproveStatus(ApproveStatusConstant.APPROVE_STATUS_AGREE.getKey());
        if (!enterpriseService.save(enterprise)) {
            throw new SysException("A00001", "保存失败！");
        }
        return enterprise.getId();
    }
}
